package controllers

import (
	"anban/service"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"net/url"
)

type MyTestController struct {
	beego.Controller
}

func (c *MyTestController) Prepare() {
	c.EnableXSRF = false;
}

func (c *MyTestController) DoTask(tasks <-chan int) {
	for i := range tasks {
		logs.Info(i)
	}
}

func (c *MyTestController) Index() {

	/*workerNum := 5
	tasks := make(chan int, 100)
	for i := 0; i < workerNum; i++ {
		go c.DoTask(tasks)
	}
	for i := 1; i <= 20; i++ {
		tasks <- i
	}
	c.Ctx.WriteString("ok")*/

	/*enterprisePayment := wechat.NewEnterprisePayment("o-DJExALt1PTmIzspxD-BegvI1kc", "2020120312093943432", 100)
	res, _ := enterprisePayment.Pay()
	logs.Info(res)
	c.Ctx.WriteString("ok")*/

	//res := wechat.CreateSceneQrcode("inviteparent_2")
	//c.Ctx.WriteString(fmt.Sprintf("ticket:%s expire_seconds:%d url:%s", res.Ticket, res.ExpireSeconds, res.Url))

	/*mUrl := "https://public-1300796766.cos.ap-guangzhou.myqcloud.com/admin/20200222/0f8f08f3f4bb790d5de9c8de6e743532.jpg"
	u, _ := url.Parse(mUrl)
	c.Ctx.WriteString(strings.Trim(u.Path, "/"))*/
	/*voiceMsg := tencent.NewVoiceMsg(123123, "15939025192", []string{"我好了"})
	res, _ := voiceMsg.Send()
	c.Ctx.WriteString(fmt.Sprintf("%d%s", res.Result, res.ErrMsg))*/

	c.Ctx.WriteString("ok")
}

func (c *MyTestController) TimeFunc(a string) {
	logs.Info("TimeFunc:", a)
}

func (c *MyTestController) InitAdminUser() {
	p := map[string]interface{}{}
	p["account"] = "admin"
	record := service.GetUserAdminInfo(p)
	if record.Id > 0 {
		c.Ctx.WriteString("请不要重复初始化管理员")
		return
	}
	input := url.Values{
		"name": {"超级管理员"},
		"describe": {"超级管理员"},
	}
	userAdminRoleId, _ := service.AddUserAdminRole(input)
	input = url.Values{
		"user_admin_role_id": {fmt.Sprintf("%d", userAdminRoleId)},
		"account": {"admin"},
		"password": {"anban123!@"},
		"status": {"1"},
	}
	service.AddUserAdmin(input)
	c.Ctx.WriteString("管理员初始化成功，用户名admin 密码:anban123!@")
}
